import { h, resolveComponent } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import Layout from '@/layout/index.vue'

const routes = [
  {
    path: '/',
    redirect: '/main',
    component: () => import('@/App.vue')
  },
  {
    path: '/main',
    component: () => import('@/views/main/index.vue'),
    name: 'main',
    meta: { title: '主应用1' }
  },
  {
    path: '/:pathMatch(.*)*',
    component: { render: () => h(resolveComponent('router-view')) },
    meta: {
      title: '微应用'
    }
  }
]

const router = createRouter({
  history: createWebHistory(''),
  routes
})
//主应用使用的嵌套路由 
router.beforeEach((to, from, next) => {
  if (!window.history.state.current) window.history.state.current = to.fullPath
  if (!window.history.state.back) window.history.state.back = from.fullPath
  // 手动修改history的state
  return next()
})
export default router
